<?php

/**
 * This is the model class for table "{{person_date}}".
 *
 * The followings are the available columns in table '{{person_date}}':
 * @property string $pdid
 * @property integer $parent
 * @property integer $uid
 * @property string $job
 * @property string $type
 * @property integer $time
 * @property string $style
 * @property integer $status
 * @property integer $dateline
 * @property integer $updated
 */
class PersonDate extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return PersonDate the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{person_date}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('type, time', 'required'),
            array('parent, uid, time, status, dateline, updated', 'numerical', 'integerOnly' => true),
            array('job', 'length', 'max' => 100),
            array('type, style', 'length', 'max' => 5),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('pdid, parent, uid, job, type, time, style, status, dateline, updated', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'User', 'uid'),
            'tasks' => array(self::HAS_MANY, 'PersonTask', 'pdid', 'order' => 'tasks.dateline DESC'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'pdid' => 'Pdid',
            'parent' => 'Parent',
            'uid' => 'Uid',
            'job' => 'Job',
            'type' => 'Periodic',
            'time' => 'Time',
            'style' => 'Style',
            'status' => 'Status',
            'dateline' => 'Dateline',
            'updated' => 'Updated',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($status=0) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('pdid', $this->pdid, true);
        $criteria->compare('parent', $this->parent);
        $criteria->compare('uid', $this->uid);
        $criteria->compare('job', $this->job, true);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('time', $this->time);
        $criteria->compare('style', $this->style, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('dateline', $this->dateline);
        $criteria->compare('updated', $this->updated);

        $criteria->addCondition("uid=" . Yii::app()->user->id); //只能看见属于自己的personDate
        if ($status != '999') {//999代表管理员，可以看到所有的任务
            $criteria->addCondition("status=" . (int) $status); //普通列表中不能看见模板
        }
        $criteria->order = 'pdid DESC'; //排序条件

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    //可以看见所有的Job,给管理员用的
    public function adminSearch() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('pdid', $this->pdid, true);
        $criteria->compare('uid', $this->uid);
        $criteria->compare('job', $this->job, true);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('time', $this->time);
        $criteria->compare('style', $this->style, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('dateline', $this->dateline);
        $criteria->compare('updated', $this->updated);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    //可以看见本部门的可见Job,给otherAdmin用的
    public function otherSearch() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('pdid', $this->pdid, true);
        $criteria->compare('uid', $this->uid);
        $criteria->compare('job', $this->job, true);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('time', $this->time);
        $criteria->compare('style', $this->style, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('dateline', $this->dateline);
        $criteria->compare('updated', $this->updated);

        $criteria->with = 'user';
        $criteria->addCondition("user.office='" . Yii::app()->user->office . "'"); //只能取得和当前用户同一office的
        $criteria->addCondition("user.read=0"); //用户定义为可见的
        $criteria->order = 'pdid DESC'; //排序条件

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = time();
                $this->uid = Yii::app()->user->id;
            }

            $this->updated = time();
            if ($this->status == 100) {//模板的情况
                $this->job = Yii::app()->user->username . '_' . $this->type . 'tt' . $this->style;
            } else {
                switch ($this->type) {
                    case 'S':
                        $this->job = Yii::app()->user->username . '_' . $this->type . $this->time . $this->style;
                        break;
                    case 'O':
                        $this->job = Yii::app()->user->username . '_' . $this->type . $this->time . $this->style;
                        break;
                    case 'D':
                        $this->job = Yii::app()->user->username . '_' . $this->type . $this->time . $this->style;
                        break;
                    case 'W':
                        $this->job = Yii::app()->user->username . '_' . $this->type . date('Y', strtotime($this->time)) . date('W', strtotime($this->time)) . $this->style;
                        break;
                    case 'M':
                        $this->job = Yii::app()->user->username . '_' . $this->type . date('Ym', strtotime($this->time)) . $this->style;
                        break;
                    case 'Y':
                        $this->job = Yii::app()->user->username . '_' . $this->type . date('Y', strtotime($this->time)) . $this->style;
                        break;
                    default:
                }
            }



            return true;
        } else {
            return false;
        }
    }

}